package www.reagnetbox.com.scan;

import android.content.Context;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class UsbHidDriver  implements CommDriver {
	UsbHidAbility mComm;
	Context mContext;
	UsbCommDev mDev;

	public static final int DEFAULT_READ_BUFFER_SIZE = 16 * 1024;
	public static final int DEFAULT_WRITE_BUFFER_SIZE = 16 * 1024;

	public UsbHidDriver(Context context, CommType dev) {
		mDev = (UsbCommDev) dev;
		mContext = context;
		mComm = new UsbHidAbility(mContext, mDev);
	}

	@Override
	public List<CommTypeDescript> getSupportCommType() {
		List<CommTypeDescript> list = new ArrayList<>();
		list.add(CommTypeDescript.USB_HID);
		return list;
	}

	@Override
	public CommAbility getCommAbility() {

		return mComm;
	}


	class UsbHidAbility extends UsbAbility implements UsbSerialConst {



		public UsbHidAbility(Context context, UsbCommDev dev) {
			super(context, dev);
		}


		@Override
		public void open() throws IOException {

			LogComm.printf("hidpos open");
			try {
				super.open();
				if (usbInterface == null || usbDevice == null) {
					throw new IOException("not find cdc usb");
				}
			} catch (IOException e) {
				throw e;
			}
		}


		@Override
		public void close() throws IOException {

			if (deviceConnection == null) {
				throw new IOException("Already closed");
			}

			deviceConnection.releaseInterface(usbInterface);
			super.close();

		}


	}
}